using AvalonAdmin.BaseViewModel;
using AvalonAdmin.Helpers;
using AvalonAdminDto.CarVin.VINDtos;
using AvalonAdminDto.Common;

using D.UtilCore.Dtos;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;

namespace AvalonAdmin.Pages.CarVin.ViewModels
{
    /// <summary>
    /// VIN码修改视图模型
    /// </summary>
    public class VINModifyViewModel : BaseModifyViewModel
    {
        #region 字段
        private string? _id;
        private string? _vin;
        private string? _vehicleModel;
        private string? _engineModel;
        private string? _transmissionModel;
        private string? _remark;
        private ViewStatus _viewStatus;
        #endregion

        #region 属性
        /// <summary>
        /// ID
        /// </summary>
        public string? Id
        {
            get => _id;
            set => SetProperty(ref _id, value);
        }

        /// <summary>
        /// VIN码
        /// </summary>
        [Required(ErrorMessage = "VIN码不能为空")]
        [StringLength(17, MinimumLength = 17, ErrorMessage = "VIN码必须为17位")]
        public string? VIN
        {
            get => _vin;
            set => SetProperty(ref _vin, value);
        }

        /// <summary>
        /// 车型
        /// </summary>
        public string? VehicleModel
        {
            get => _vehicleModel;
            set => SetProperty(ref _vehicleModel, value);
        }

        /// <summary>
        /// 发动机型号
        /// </summary>
        public string? EngineModel
        {
            get => _engineModel;
            set => SetProperty(ref _engineModel, value);
        }

        /// <summary>
        /// 变速箱型号
        /// </summary>
        public string? TransmissionModel
        {
            get => _transmissionModel;
            set => SetProperty(ref _transmissionModel, value);
        }

        /// <summary>
        /// 备注
        /// </summary>
        public string? Remark
        {
            get => _remark;
            set => SetProperty(ref _remark, value);
        }

        /// <summary>
        /// 视图状态
        /// </summary>
        public ViewStatus ViewStatus
        {
            get => _viewStatus;
            set => SetProperty(ref _viewStatus, value);
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="model">VIN数据模型</param>
        /// <param name="viewStatus">视图状态</param>
        public VINModifyViewModel(RepVINInfo? model = null, ViewStatus viewStatus = ViewStatus.Add)
        {
            ViewStatus = viewStatus;
            if (model != null)
            {
                LoadData(model);
            }
        }
        #endregion

        #region 重写方法
        /// <summary>
        /// 提交数据
        /// </summary>
        protected override async Task SubmitAsync()
        {
            try
            {
                // 验证数据
                if (string.IsNullOrWhiteSpace(VIN))
                {
                    ShowWarning("请输入VIN码");
                    return;
                }

                if (VIN.Length != 17)
                {
                    ShowWarning("VIN码必须为17位");
                    return;
                }

                // 构建请求数据
                var requestData = new RepVINInfo
                {
                    Id = Id,
                    VIN = VIN,
                    VehicleModel = VehicleModel,
                    EngineModel = EngineModel,
                    TransmissionModel = TransmissionModel,
                    Remark = Remark
                };

                ApiResult<int> res;
                if (ViewStatus == ViewStatus.Add)
                {
                    // 添加
                    res = await HttpHelper<int>.PostAsync(ApiUrl.AddVINInfo, requestData);
                    HandleApiResponse(res, "添加成功", "添加失败");
                }
                else
                {
                    // 更新
                    res = await HttpHelper<int>.PutAsync(ApiUrl.UpdateVINInfo, requestData);
                    HandleApiResponse(res, "更新成功", "更新失败");
                }
            }
            catch (System.Exception ex)
            {
                ShowError($"操作失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 重置表单
        /// </summary>
        protected override void ResetForm()
        {
            if (ViewStatus == ViewStatus.Add)
            {
                Id = null;
                VIN = string.Empty;
                VehicleModel = string.Empty;
                EngineModel = string.Empty;
                TransmissionModel = string.Empty;
                Remark = string.Empty;
            }
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="model">数据模型</param>
        private void LoadData(RepVINInfo model)
        {
            Id = model.Id;
            VIN = model.VIN;
            VehicleModel = model.VehicleModel;
            EngineModel = model.EngineModel;
            TransmissionModel = model.TransmissionModel;
            Remark = model.Remark;
        }
        #endregion
    }
}